import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list = new ArrayList<>();
        HashMap<String, Integer> hash1 = new LinkedHashMap<>();
        int m = words.length;
        int len = words[0].length();
        for (int i = 0; i < m; i++) {
            String str = words[i];
            hash1.put(str, hash1.getOrDefault(str, 0) + 1);

        }
        for (int i = 0; i < len; i++) {
            HashMap<String, Integer> hash2 = new LinkedHashMap<>();
            int count = 0;
            for (int left = i, right = i; right + len <= s.length(); right += len) {
                String str = s.substring(right, right + len);
                hash2.put(str, hash2.getOrDefault(str, 0) + 1);

                if (hash2.get(str) <=hash1.getOrDefault(str,0)) {
                    count++;
                }
                if (right - left + 1 > m * len) {
                    String out = s.substring(left, left + len);

                    if (hash2.get(out) <=hash1.getOrDefault(out,0)) {
                        count--;
                    }
                    hash2.put(out, hash2.getOrDefault(out, 0) - 1);
                    left += 3;
                }
                if (count == m) {
                    list.add(left);
                }
            }
        }

                  return list;
    }
}